package com.sctvcloud.qingchuan.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.text.TextUtils;
import android.util.SparseArray;

import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.beans.ServiceBean;
import com.sctvcloud.qingchuan.beans.TopicSubBean;
import com.sctvcloud.qingchuan.ui.fragment.ServiceColumnFragment;
import com.sctvcloud.qingchuan.ui.fragment.SubTopicNewsListFragment;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * 新版专题详情底部加载子专题tab
 */
public class TopicNewDetailAdapter extends FragmentStatePagerAdapter {
    private List<TopicSubBean> subTopicList;
    private SparseArray<WeakReference<SubTopicNewsListFragment>> fragmentWeaks;

    public TopicNewDetailAdapter(FragmentManager fm, List<TopicSubBean> list) {
        super(fm);
        subTopicList = list;
        fragmentWeaks = new SparseArray<>();
    }

    public void setSubTopicList(List<TopicSubBean> list) {
        this.subTopicList = list;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        SubTopicNewsListFragment fragment = new SubTopicNewsListFragment();
        fragment.setTopicData(subTopicList.get(position));
        fragment.setViewTitle(true);
//        fragment.setServiceUsedListener(listener);
        fragment.setIndexInViewPager(position);
        fragmentWeaks.put(position, new WeakReference<SubTopicNewsListFragment>(fragment));
        return fragment;
    }

//    @Override
//    public CharSequence getPageTitle(int position) {
//        if (!TextUtils.isEmpty(subTopicList.get(position).getSubTopicTitle())) {
//            if (subTopicList.get(position).getSubTopicTitle().length() <= 6) {
//                return subTopicList.get(position).getSubTopicTitle();
//            } else {
//                return subTopicList.get(position).getSubTopicTitle().substring(0, 6) + "...";
//            }
//        } else {
//            return "";
//        }
//    }

    @Override
    public int getCount() {
        if (ListUtils.isListValued(subTopicList)) {
            return subTopicList.size();
        } else {
            return 0;
        }
    }

}
